package cn.fsd.service.impl;

import cn.fsd.domain.Customer;
import cn.fsd.domain.Customer_address;
import cn.fsd.exception.CustomerException;
import cn.fsd.exception.CustomerExceptionCodeMsg;
import cn.fsd.mapper.CustomerMapper;
import cn.fsd.mapper.Customer_addressMapper;
import cn.fsd.mapper.OrderMapper;
import cn.fsd.query.customer.CustomerQuery;
import cn.fsd.service.CustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class CustomerServiceImpl implements CustomerService {
    final CustomerMapper customerMapper;
    final OrderMapper orderMapper;

    final Customer_addressMapper addressMapper;

    @Autowired
    public CustomerServiceImpl(CustomerMapper customerMapper, OrderMapper orderMapper, Customer_addressMapper addressMapper) {
        this.customerMapper = customerMapper;
        this.orderMapper = orderMapper;
        this.addressMapper = addressMapper;
    }

    @Override
    public PageInfo<Customer> findByConditionByPage(CustomerQuery customerQuery) {
        PageHelper.startPage(customerQuery.getPageNow(), customerQuery.getPageSize());
        List<Customer> customerInfoBeanList = customerMapper.selectByCondition(customerQuery);
        return new PageInfo<>(customerInfoBeanList);
    }

    @Override
    public List<String> completeInfo() {
        return customerMapper.completeInfo();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCustomer(Customer customer) throws CustomerException {
        customer.setCustomer_id(String.valueOf(UUID.randomUUID()));
        Customer_address customer_address = new Customer_address(customer.getCustomer_id(),
                0, customer.getCustomer_name(),
                customer.getMobile_phone(), customer.getPostal_code(),
                customer.getContact_place());
        if (customerMapper.insert(customer) == 0) {
            throw new CustomerException(CustomerExceptionCodeMsg.CUSTOMER_INSERT_FAIL);
        }
        if (addressMapper.insert(customer_address) == 0) {
            throw new CustomerException(CustomerExceptionCodeMsg.ADDRESS_INSERT_FAIL);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCustomer(String id) throws CustomerException {
        if (orderMapper.selectByCustomerId(id).isEmpty()) {
            if(customerMapper.deleteByPrimaryKey(id)==0){
                throw new CustomerException(CustomerExceptionCodeMsg.CUSTOMER_DELETE_FAIL);
            }
            if(addressMapper.deleteByCustomerId(id)==0){
                throw new CustomerException(CustomerExceptionCodeMsg.ADDRESS_DELETE_FAIL);
            }
        } else {
            throw new CustomerException(CustomerExceptionCodeMsg.CUSTOMER_DELETE_FAIL_ASSOCIATION);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCustomer(Customer customer) throws CustomerException {
        if (customerMapper.updateByPrimaryKey(customer) == 0) {
            throw new CustomerException(CustomerExceptionCodeMsg.CUSTOMER_UPDATE_FAIL);
        }
        Customer_address address=addressMapper.selectByPrimaryKey(customer.getCustomer_id(),0);
        address.setReceiver(customer.getCustomer_name());
        address.setReceiver_address(customer.getContact_place());
        address.setReceiver_phone(customer.getMobile_phone());
        address.setReceiver_postal_code(customer.getPostal_code());
        if (addressMapper.updateByPrimaryKey(address) == 0) {
            throw new CustomerException(CustomerExceptionCodeMsg.ADDRESS_UPDATE_FAIL);
        }
    }

    @Override
    public Customer getCustomer(String id) {
        return customerMapper.selectByPrimaryKey(id);
    }

}
